package com.AAA.service.Impl.fpa;

import com.AAA.entity.MyPage;
import com.AAA.entity.fpa.TFpa;
import com.AAA.service.fpa.ITFpaService;
import com.AAA.dao.fpa.ITFpaMapper;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * (TFpa)表服务实现类
 *
 * @author 何泽明
 * @since 2022-11-05 12:08:35
 */
@Service
public class ITFpaServiceImpl implements ITFpaService {
   @Autowired
   private ITFpaMapper mapper;

   //批量添加题目
   @Override
   public Integer addAll(TFpa tFpa) {
      int insert = mapper.insert(tFpa);
      if (insert>0) {
         return 1;
      }
      return 0;
   }

   //添加单个题目到数据库
   @Override
   public Integer add(TFpa tFpa) {
      int insert = mapper.insert(tFpa);
      if (insert>0) {
         return 1;
      }
      return 0;
   }

   //删除数据库题目
   @Override
   public Integer delete(Integer qid) {
      int i = mapper.deleteByPrimaryKey(qid);
      if (i > 0){
         return 1;
      }
      return 0;
   }

   //编辑——修改，进行判断
   @Override
   public boolean edit(TFpa tFpa) {
      //updateByPrimaryKeySelective对字段进行判断再更新(如果为Null就忽略更新)，如果你只想更新某一字段
      int i = mapper.updateByPrimaryKeySelective(tFpa);
      if (i > 0){
         return true;
      }
      return false;
   }

   //分页查询，对TSas数据库题目进行分页查询
   @Override
   public PageInfo query(MyPage myPage, TFpa tFpa) {
      PageHelper.startPage(myPage.getPage(),myPage.getLimit());
      List<TFpa> tFpas = mapper.queryAll(tFpa);
      PageInfo pageInfo = new PageInfo(tFpas);
      return pageInfo;
   }

   @Override
   public void dealFile(MultipartFile file) {

      // 如果试题已经存在就不让再导入了
      int i = mapper.selectCount(null);
      if(i>0){
         return;
      }else {
         // 将excel中的数据导入到数据库中
//      InputStream is = null;
//         String fileUrl="https://yuyonglitest.oss-cn-beijing.aliyuncs.com/%E9%A2%98%E7%9B%AE%E6%B8%85%E5%8D%952.xlsx";
//      try {
//         is = new URL(fileUrl).openStream();
//      } catch (IOException e) {
//         e.printStackTrace();
//      }
         InputStream is = null;
         try {
            is = file.getInputStream();
         } catch (IOException e) {
            e.printStackTrace();
         }
         //1.进行读取数据，TFpaSub是我的pojo类，
         //2.new FileListener(fileService)这个是监听器，主要用来读取数据的
         //3.特别注意的是fileService这个service，我上面有注入进去 @Autowired/@Resource，切记不要new会报错
         //EasyExcel.read(is, TFpaSub.class, new FileListener(fileService)).sheet("FPA性格测试").doRead();
         EasyExcel.read(is, TFpa.class, new AnalysisEventListener<TFpa>() {
            /**
             * 单次缓存的数据量
             */
            public static final int BATCH_COUNT = 100;
            /**
             * 临时存储
             */
            private List<TFpa> cachedDataList = new ArrayList<>();

            @Override
            public void invoke(TFpa data, AnalysisContext context) {
               cachedDataList.add(data);
               if (cachedDataList.size() >= BATCH_COUNT) {
                  saveData();
                  // 存储完成清理 list
                  cachedDataList = new ArrayList<>();
               }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
               saveData();
            }

            /**
             * 加上存储数据库
             */
            private void saveData() {
//            log.info("{}条数据，开始存储数据库！", cachedDataList.size());
//            log.info("存储数据库成功！");
               save(cachedDataList);
            }
         }).sheet("FPA性格测试").doRead();
      }
   }

   @Override
   public void save(List<TFpa> list) {
      mapper.insertFpaSubjects(list);
   }

   @Override
   public PageInfo getAll(MyPage myPage) {
      PageHelper.startPage(myPage.getPage(),myPage.getLimit());
      return new PageInfo(mapper.selectAll());
   }
}

